package com.ly.hotel.view.fragments.music;

import android.app.Dialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.MusicBean;
import com.ly.hotel.msgevent.MusicPlayerEvent;

import java.io.IOException;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by SHUBEN on 2018/1/26.
 */

public class MusicPlayerListDialog extends Dialog {
    private MyApplication app;
    private MusicActivity mcontext;
    private List<MusicBean> mAlbumbean;
    private TextView listMusicNum;// 例：全部歌曲(70)首
    private ImageButton listDeleteAll;// 全部删除
    private ListView musicListView;
    private ImageView mdownMore;
    private MusicPlayListAdapter musicListAdapter;


    public MusicPlayerListDialog(MusicActivity context) {
        super(context);
        this.mcontext = context;
    }

    public MusicPlayerListDialog(MusicActivity context, int theme, List<MusicBean> lb) {
        super(context, theme);
        this.mcontext = context;
        this.mAlbumbean = lb;
        app = (MyApplication) mcontext.getApplicationContext();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_playerlist);
        EventBus.getDefault().register(this);
        listMusicNum = (TextView) findViewById(R.id.m_playerlist_num);
        listDeleteAll = (ImageButton) findViewById(R.id.m_remove_all_btn);
        musicListView = (ListView) findViewById(R.id.musicListView);
        mdownMore = (ImageView) findViewById(R.id.music_list_more);
        Button backBtn = (Button) findViewById(R.id.dialog_back);
        backBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dismiss();
            }
        });

        musicListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView arg0, int arg1) {
                // TODO Auto-generated method stub
            }

            /* (non-Javadoc)
             * @see android.widget.AbsListView.OnScrollListener#onScroll(android.widget.AbsListView, int, int, int)
             */
            @Override
            public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {
                // TODO Auto-generated method stub
                if ((arg1 == 0 && arg2 == arg3) || (arg1 > 0 && arg1+arg2 == arg3)) {
                    mdownMore.setVisibility(View.INVISIBLE);
                }  else {
                    mdownMore.setVisibility(View.VISIBLE);
                }
            }
        });
        setMusicToAlbumMusicList(mAlbumbean);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void setMusicToAlbumMusicList(List<MusicBean> mb) {
        musicListAdapter = new MusicPlayListAdapter(mcontext);
        musicListAdapter.setDateList(mb);
        setListNum();

        // 使用自定义的Adapter
        musicListView.setItemsCanFocus(true);
        musicListView.setAdapter(musicListAdapter);

        listDeleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mcontext.clearMusicPlayList();
                setListNum();
                musicListAdapter.notifyDataSetChanged();
            }
        });

        //请求焦点
        musicListView.post(new Runnable(){
            public void run(){
                int count = musicListView.getChildCount();
                if (count != 0){
                    musicListView.setSelection(0);
                    musicListView.getChildAt(0).findViewById(R.id.m_music_album_music_list_btn).requestFocus();
                    musicListView.getChildAt(0).findViewById(R.id.m_music_album_music_list_btn).requestFocusFromTouch();
                }
            }

        });
    }

    public void updataPlayerList(int position){
        if (position >= 0 && position < MyApplication.musicPlayList.size()) {
            MyApplication.musicPlayList.remove(position);
        }
        setListNum();
        musicListAdapter.notifyDataSetChanged();
        musicListView.post(new Runnable(){
            public void run(){
                int count = musicListView.getChildCount();
                if (count != 0){
                    musicListView.requestFocus();
                    musicListView.requestFocusFromTouch();
                }
            }

        });
    }

    private void setListNum(){
        listMusicNum.setText("（" + MyApplication.musicPlayList.size() + "）");
    }

    @Override
    public boolean onKeyDown(int keyCode,KeyEvent event) {
        return mcontext.onKeyDown(keyCode,event);
    }

    public void onEventMainThread(MusicPlayerEvent e) throws IllegalStateException, IOException {
        if (e.getCtlCode() == 0) {
            int pos = e.getIndex();
            updataPlayerList(pos);
        }
    }
}